package cn.glenn._03vehicle.controller;

import cn.glenn._03vehicle.base.response.JsonResult;
import cn.glenn._03vehicle.mapper.NoticeMapper;
import cn.glenn._03vehicle.pojo.dto.NoticeAddParam;
import cn.glenn._03vehicle.pojo.enity.Notice;
import cn.glenn._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("v1/notice/")
@Slf4j
@Tag(name = "03-公告管理",description = "03-公告管理",extensions ={
        @Extension(properties = {@ExtensionProperty(name = "x-order", value = "3", parseValue = true)}
        )})
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @Operation(summary = "添加资讯")
    public JsonResult noticeList(@RequestBody NoticeAddParam noticeAddParam, HttpSession session) {
        if (session.getAttribute("user") == null) {return JsonResult.notLogin();}
        Notice notice = new Notice();
        UserVO user = (UserVO) session.getAttribute("user");
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId((long)user.getId());
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return JsonResult.success(notice);
    }

    @GetMapping("list")
    @Operation(summary = "资讯列表")
    public JsonResult noticeList(){
        return JsonResult.success(noticeMapper.selectNotice());
    }
}
